package com.gs.SynPatrolRep.fragment.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.gs.SynPatrolRep.R;
import com.gs.SynPatrolRep.control.ToolBarSearch;
import com.gs.SynPatrolRep.control.ToolBarTitle;
import com.gs.SynPatrolRep.db.query.BaseQuery;
import com.gs.SynPatrolRep.constant.AllConstants;
import com.gs.SynPatrolRep.presenter.BasePresenter;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseListFragment<V, T extends BasePresenter<V>, D, Q extends BaseQuery> extends Fragment {
    protected Context context;
    private View view;
    protected T mPresenter;
    protected List<D> list;
    protected Q query;
    protected int totalPages;
    protected SmartRefreshLayout smartRefreshLayout;
    protected RecyclerView recyclerView;
    protected ToolBarSearch toolBarSearch;
    protected ToolBarTitle toolBarTitle;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(getLayoutId(), container, false);
        context = getActivity();
        mPresenter = createPresenter();
        query = createQuery();
        list = new ArrayList<>();
        if (mPresenter != null) {
            mPresenter.attachView((V) this);
        }
        recyclerView = fd(R.id.recyclerview);
        smartRefreshLayout = fd(R.id.refresh);
        if (smartRefreshLayout != null) {
            // 刷新
            smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                    smartRefreshLayout.setTag(AllConstants.REFRESH);
                    list.clear();
                    if (query != null) {
                        query.setPageIndex(1);
                        loadData();
                    }
                }
            });
            // 加載更多
            smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                    smartRefreshLayout.setTag(AllConstants.LOAD_MORE);
                    if (query != null) {
                        if (totalPages == 0) {
                            totalPages = 1;
                        }
                        if (totalPages <= query.getPageIndex()) {
                            query.setPageIndex(++totalPages);
                        } else {
                            query.setPageIndex(query.getPageIndex() + 1);
                        }
                        loadData();
                    }
                }
            });
        }
        toolBarTitle = fd(R.id.toolBarTitle);
        if (toolBarTitle != null) {
            toolBarTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (v.getId() == R.id.item_per) {
                        filter();
                    }
                }
            });
        }
        toolBarSearch = fd(R.id.toolBarSearch);
        if (toolBarSearch != null) {
            toolBarSearch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (v.getId() == R.id.item_per) {
                        search();
                    }
                }
            });
        }
        initView();
        search();
        return view;
    }

    protected abstract int getLayoutId();

    protected abstract T createPresenter();

    protected abstract Q createQuery();

    protected abstract void initView();

    protected abstract void loadData();

    protected abstract void search();

    public abstract void filter();

    /**
     * 更新SmartRefreshLayout狀態
     */
    public void refreshSmartRefreshLayout() {
        if (smartRefreshLayout.getTag() == null) {
            return;
        }
        if (smartRefreshLayout.getTag().equals(AllConstants.REFRESH)) {
            smartRefreshLayout.finishRefresh();
        } else {
            smartRefreshLayout.finishLoadMore();
        }
    }

    protected <I extends View> I fd(int id) {
        return view.findViewById(id);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }
}
